package com.wzh.codegen.biz.controller;

import com.wzh.codegen.api.domain.dto.TableConfigQuery;
import com.wzh.codegen.biz.domain.dto.CodegenTableConfigDTO;
import com.wzh.codegen.biz.service.CodegenTableConfigService;
import com.wzh.common.general.result.PageQuery;
import com.wzh.common.general.result.PageResult;
import com.wzh.common.general.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 代码生成表配置控制器
 *
 * @author wzh
 * @since 2024-01-01
 */
@Tag(name = "表配置管理", description = "代码生成表配置相关接口")
@RestController
@RequestMapping("/table/configs")
@RequiredArgsConstructor
public class CodegenTableConfigController {

    private final CodegenTableConfigService tableConfigService;

    @Operation(summary = "保存表配置")
    @PostMapping
    public Result<Long> saveTableConfig(@Valid @RequestBody CodegenTableConfigDTO dto) {
        Long id = tableConfigService.saveTableConfig(dto);
        return Result.success(id);
    }

    @Operation(summary = "更新表配置")
    @PutMapping("/{id}")
    public Result<Void> updateTableConfig(
            @Parameter(description = "配置ID") @PathVariable("id") Long id,
            @Valid @RequestBody CodegenTableConfigDTO dto) {
        dto.setId(id);
        tableConfigService.updateTableConfig(dto);
        return Result.success();
    }

    @Operation(summary = "获取表配置列表")
    @GetMapping
    public Result<List<CodegenTableConfigDTO>> getTableConfigs(
            @Parameter(description = "数据源ID") @RequestParam(name = "datasourceId", required = false) Long datasourceId) {
        List<CodegenTableConfigDTO> configs = tableConfigService.getTableConfigs(datasourceId);
        return Result.success(configs);
    }

    @Operation(summary = "分页查询表配置")
    @PostMapping("/page")
    public Result<PageResult<CodegenTableConfigDTO>> pageTableConfigs(@RequestBody PageQuery<TableConfigQuery> pageQuery) {
        PageResult<CodegenTableConfigDTO> result = tableConfigService.pageTableConfigs(pageQuery);
        return Result.success(result);
    }

    @Operation(summary = "获取表配置详情")
    @GetMapping("/{id}")
    public Result<CodegenTableConfigDTO> getTableConfig(
            @Parameter(description = "配置ID") @PathVariable("id") Long id) {
        CodegenTableConfigDTO config = tableConfigService.getTableConfig(id);
        return Result.success(config);
    }

    @Operation(summary = "删除表配置")
    @DeleteMapping("/{id}")
    public Result<Void> deleteTableConfig(
            @Parameter(description = "配置ID") @PathVariable("id") Long id) {
        tableConfigService.deleteTableConfig(id);
        return Result.success();
    }

    @Operation(summary = "批量删除表配置")
    @DeleteMapping("/batch")
    public Result<Void> batchDeleteTableConfigs(
            @RequestBody List<Long> ids) {
        tableConfigService.batchDeleteTableConfigs(ids);
        return Result.success();
    }

    @Operation(summary = "从数据库导入表配置")
    @PostMapping("/import/{datasourceId}/{tableName}")
    public Result<Long> importTableFromDatabase(
            @PathVariable("datasourceId") Long datasourceId,
            @PathVariable("tableName") String tableName) {
        Long configId = tableConfigService.importTableFromDatabase(datasourceId, tableName);
        return Result.success(configId);
    }

    @Operation(summary = "根据数据源和表名获取配置")
    @GetMapping("/datasource/{datasourceId}/table/{tableName}")
    public Result<CodegenTableConfigDTO> getTableConfigByDatasourceAndTable(
            @Parameter(description = "数据源ID") @PathVariable("datasourceId") Long datasourceId,
            @Parameter(description = "表名") @PathVariable("tableName") String tableName) {
        CodegenTableConfigDTO config = tableConfigService.getTableConfigByDatasourceAndTable(datasourceId, tableName);
        return Result.success(config);
    }
}